#include "msgeditarea.h"
#include "./model/datacenter.h"
#include "toast.h"
#include "mainwidget.h"

MsgEditArea::MsgEditArea(QWidget *parent)
{
    this->setFixedHeight(200);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 创建垂直方向布局管理器
    QVBoxLayout *vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(10, 0, 10, 10);
    this->setLayout(vlayout);

    // 创建水平方向布局管理器
    QHBoxLayout *hlayout = new QHBoxLayout();
    hlayout->setSpacing(10);
    hlayout->setContentsMargins(10, 0, 0, 0);
    hlayout->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    vlayout->addLayout(hlayout);

    // 创建上方四个按钮
    QString btnStyle = "QPushButton { background-color: rgb(245, 245, 245); border: none; background: transparent; } QPushButton:pressed { background-color: rgb(200, 200, 200); }";
    _sendImageBtn = new QPushButton();
    _sendFileBtn = new QPushButton();
    _sendSpeechBtn = new QPushButton();
    _showHistoryBtn = new QPushButton();

    _sendImageBtn->setStyleSheet(btnStyle);
    _sendFileBtn->setStyleSheet(btnStyle);
    _sendSpeechBtn->setStyleSheet(btnStyle);
    _showHistoryBtn->setStyleSheet(btnStyle);

    _sendImageBtn->setFixedSize(25, 25);
    _sendImageBtn->setIconSize(QSize(25, 25));
    _sendFileBtn->setFixedSize(25, 25);
    _sendFileBtn->setIconSize(QSize(25, 25));
    _sendSpeechBtn->setFixedSize(25, 25);
    _sendSpeechBtn->setIconSize(QSize(25, 25));
    _showHistoryBtn->setFixedSize(25, 25);
    _showHistoryBtn->setIconSize(QSize(25, 25));

    _sendImageBtn->setIcon(QIcon(":/resource/image/image.png"));
    _sendFileBtn->setIcon(QIcon(":/resource/image/file.png"));
    _sendSpeechBtn->setIcon(QIcon(":/resource/image/sound.png"));
    _showHistoryBtn->setIcon(QIcon(":/resource/image/history.png"));

    hlayout->addWidget(_sendImageBtn);
    hlayout->addWidget(_sendFileBtn);
    hlayout->addWidget(_sendSpeechBtn);
    hlayout->addWidget(_showHistoryBtn);

    // 添加多行编辑框
    _textEdit = new QPlainTextEdit();
    _textEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    _textEdit->setStyleSheet("QPlainTextEdit { border: none; background-color: transparent; font-size: 14px; padding: 10px; }");
    _textEdit->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 1px; background-color: rgb(245, 245, 245); }");
    vlayout->addWidget(_textEdit);

    // 添加发送按钮
    _sendTextBtn = new QPushButton();
    _sendTextBtn->setText("发送");
    _sendTextBtn->setFixedSize(100, 40);
    QString style = "QPushButton { font-size: 16px; color: rgb(7, 193, 96); border: none; background-color: rgb(233, 233, 233); border-radius: 10px; }";
    style += "QPushButton:hover {  background-color: rgb(210, 210, 210); }";
    style += "QPushButton:pressed { background-color: rgb(190, 190, 190); }";
    _sendTextBtn->setStyleSheet(style);
    vlayout->addWidget(_sendTextBtn, 0, Qt::AlignRight | Qt::AlignVCenter);

    // 统一初始化信号槽
    initSignalSlot();
}

void MsgEditArea::initSignalSlot()
{
    model::DataCenter *dataCenter = model::DataCenter::getInstance();
    // 显示历史消息按钮
    connect(_showHistoryBtn, &QPushButton::clicked, this, [=](){
        HistoryMessageWidget *hisMsgWidget = new HistoryMessageWidget(this);
        hisMsgWidget->exec();
    });

    // 发送文本消息按钮
    connect(_sendTextBtn, &QPushButton::clicked, this, [=](){
        sendTextMessage();
    });

    // 关联发送文本消息信号槽
    connect(dataCenter, &model::DataCenter::sendMessageDone, this, &MsgEditArea::addSelfMessage);

    // 关联收到消息信号槽
    connect(dataCenter, &model::DataCenter::receiveMessageDone, this, &MsgEditArea::addOtherMessage);
}

void MsgEditArea::sendTextMessage()
{
    // 确认当前是否选中了某个会话
    model::DataCenter *dataCenter = model::DataCenter::getInstance();
    if (dataCenter->getCurrentSessionId().isEmpty())
    {
        DEBUG() << "没有选中聊天会话, 无法发送消息";
        Toast::showMessage("没有选中聊天会话, 无法发送消息");
        return;
    }

    // 判断输入框是否为空
    QString text = _textEdit->toPlainText();
    if (text.isEmpty())
    {
        DEBUG() << "输入框为空";
        return;
    }

    // 清空输入框已有内容
    _textEdit->clear();

    // 通过网络发送消息
    dataCenter->sentTextMessageAsync(dataCenter->getCurrentSessionId(), text);
}

void MsgEditArea::addSelfMessage(model::MessageType msgType, const QByteArray &content, const QString &extraInfo)
{
    model::DataCenter *dataCenter = model::DataCenter::getInstance();
    // 构造出消息对象
    MessageInfo msg = MessageInfo::makeMessage(msgType, dataCenter->getCurrentSessionId(), *dataCenter->getMyself(), content, extraInfo);
    dataCenter->addMessage(msg);
    // 显示到消息展示区
    MainWidget *mainWidget = MainWidget::getInstance();
    mainWidget->getMsgShowArea()->addMsg(false, msg);
    mainWidget->getMsgShowArea()->scrollToEnd();

    emit dataCenter->updateLastMsg(msg._chat_session_id);
}

void MsgEditArea::addOtherMessage(const MessageInfo &message)
{
    // 显示到消息展示区
    MainWidget *mainWidget = MainWidget::getInstance();
    mainWidget->getMsgShowArea()->addMsg(true, message);
    mainWidget->getMsgShowArea()->scrollToEnd();
    Toast::showMessage("收到一个新消息");
}
